package org.example.acm;

import java.util.*;
import java.util.stream.Collectors;
//计算疫情扩散时间
public class GetInfectionDaysSolution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();

        List<Integer> list = Arrays.stream(input.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());

        System.out.println(getInfectionDays(list));

        scanner.close();
    }

    public static int getInfectionDays(List<Integer> list) {
        int n = (int) Math.sqrt(list.size());
        int[][] matrix = new int[n][n];
        Queue<int[]> queue = new LinkedList<>();
        int healthy = 0;
        for (int i=0;i<list.size();i++) {
            int row = i / n;
            int col = i % n;
            matrix[row][col] = list.get(i);
            if (matrix[row][col] == 0) {
                healthy++;
            } else {
                queue.offer(new int[]{row,col});
            }
        }
        if (healthy == 0 || healthy == n*n) {
            return -1;
        }
        int res = 0;
        int[][] directions = {{1,1}, {1,-1}, {-1,1}, {-1,-1}};
        while (!queue.isEmpty() && healthy > 0) {
            res++;
            int batchSize = queue.size();
            for (int i=0;i<batchSize;i++) {
                int[] pos = queue.poll();
                for (int[] direction: directions) {
                    int newRow = pos[0] + direction[0];
                    int newCol = pos[1] + direction[1];
                    if (newRow>=0 && newRow<n && newCol>=0 && newCol<n && matrix[newRow][newCol] == 0) {
                        healthy--;
                        queue.offer(new int[]{newRow, newCol});
                    }
                }
            }
        }
        return res;
    }


}
